import {
  Body,
  Delete,
  Get,
  HttpStatus,
  OnModuleInit,
  Param,
  Patch,
  Post,
  Req,
  Res,
} from "@nestjs/common";

export abstract class BaseController implements OnModuleInit {
  public service: any;

  @Get()
  async pageQuery(@Req() req, @Res() res) {
    const resp = await this.service.pageQuery({ ...req.query, ...req.user });
    res.status(HttpStatus.OK).json({
      ...resp,
    });
  }

  @Post()
  async insert(@Param() params, @Body() body, @Res() res) {
    const resp = await this.service.insert(body);
    res.status(HttpStatus.OK).json({
      ...resp,
      data: true,
      message: "success",
    });
  }

  @Delete(":id")
  async removeById(@Param() params, @Res() res) {
    const resp = await this.service.removeById(params.id);
    res.status(HttpStatus.OK).json({
      resp,
    });
  }

  @Patch(":id")
  async update(@Param() params, @Res() res, @Body() body) {
    const resp = await this.service.update({ id: params.id, ...body });
    res.status(HttpStatus.OK).json({
      resp,
    });
  }

  abstract init();

  onModuleInit(): any {
    this.init();
  }
}
